package org.esprit.ocm.client.ui.common;

import java.util.List;

import org.esprit.ocm.client.model.db.datasource.ServerLocalDS;
import org.esprit.ocm.client.rpc.AwsCredentialsService;
import org.esprit.ocm.client.rpc.AwsCredentialsServiceAsync;
import org.esprit.ocm.client.rpc.ServerService;
import org.esprit.ocm.client.rpc.ServerServiceAsync;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.dto.impl.UserDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;

public class ServerWindow extends Window {

	private final ServerServiceAsync			serverService		= GWT.create(ServerService.class);
	private final AwsCredentialsServiceAsync	credentialsService	= GWT.create(AwsCredentialsService.class);

	private SelectItem							selectItem;

	private ServerDto							server;
	private AwsCredentialsDto					credentials;

	private DataSource							ds;

	public ServerWindow(final UserDto u) {
		
		getData(u);

		setWidth(450);
		setHeight(350);
		setTitle("Server Window");
		setShowMinimizeButton(false);
		setIsModal(true);
		setShowModalMask(true);
		setShowCloseButton(false);
		centerInPage();
		addCloseClickHandler(new CloseClickHandler() {
			public void onCloseClick(CloseClientEvent event) {
				destroy();
			}
		});

		final SectionStack windowSectionStack = new SectionStack();
		// windowSectionStack.setTop(25);
		windowSectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);
		windowSectionStack.setWidth(450);
		windowSectionStack.setHeight(350);

		/******************************** SECTION 1 ******************************************/
		SectionStackSection serverCreationSection = new SectionStackSection("Create a server");
		serverCreationSection.setExpanded(true);
		serverCreationSection.setCanCollapse(false);
		windowSectionStack.addSection(serverCreationSection);

		VLayout windowCreationLayout = new VLayout();
		windowCreationLayout.setWidth100();
		windowCreationLayout.setHeight100();

		serverCreationSection.addItem(windowCreationLayout);

		Label titleLabel = new Label("<b>Select a server to create an account or Select your server below<b>");
		titleLabel.setPadding(5);
		titleLabel.setWidth(500);
		titleLabel.setHeight(25);

		HLayout titleLayout = new HLayout();
		titleLayout.setHeight("20%");
		titleLayout.addMember(titleLabel);

		windowCreationLayout.addMember(titleLayout);

		HLayout serverLayout = new HLayout();
		serverLayout.setHeight("80%");

		VLayout eucaLayout = new VLayout();
		eucaLayout.setWidth("25%");
		VLayout oneLayout = new VLayout();
		oneLayout.setWidth("50%");
		VLayout novaLayout = new VLayout();
		novaLayout.setWidth("25%");

		serverLayout.addMember(eucaLayout);
		serverLayout.addMember(oneLayout);
		serverLayout.addMember(novaLayout);

		ImgButton eucaButton = new ImgButton();
		eucaButton.setSrc("Eucalyptus.png");
		eucaButton.setHeight(100);
		eucaButton.setWidth(100);
		eucaButton.setShowRollOver(false);
		eucaButton.setShowDown(true);
		eucaButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();
				ds.destroy();
				ServerAccount serverAccount = new ServerAccount(u, "ec2");
				serverAccount.show();
			}
		});

		ImgButton novaButton = new ImgButton();
		novaButton.setSrc("Nova.png");
		novaButton.setHeight(100);
		novaButton.setWidth(100);
		novaButton.setShowRollOver(false);
		novaButton.setShowDown(true);
		novaButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
//				hide();
				ds.destroy();
				ServerAccount serverAccount = new ServerAccount(u, "ec2");
				serverAccount.show();
			}
		});

		ImgButton oneButton = new ImgButton();
		oneButton.setSrc("one.png");
		oneButton.setHeight(100);
		oneButton.setWidth(200);
		oneButton.setShowRollOver(false);
		oneButton.setShowDown(true);
		oneButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();
				ds.destroy();
				ServerAccount serverAccount = new ServerAccount(u, "one");
				serverAccount.show();
			}
		});

		eucaLayout.addMember(eucaButton);
		oneLayout.addMember(oneButton);
		novaLayout.addMember(novaButton);

		windowCreationLayout.addMember(serverLayout);

		/********************************* SECTION 2 ************************************************/
		SectionStackSection serverListSection = new SectionStackSection("List your servers");
		serverListSection.setExpanded(true);
		serverListSection.setCanCollapse(false);
		windowSectionStack.addSection(serverListSection);

		VLayout serverlistLayout = new VLayout();
		serverlistLayout.setWidth100();
		serverlistLayout.setHeight100();

		serverListSection.addItem(serverlistLayout);

		Label msg = new Label("You must create an account to view your servers");
		msg.setPadding(5);
		msg.setWidth(500);
		msg.setHeight(25);

		serverListSection.addItem(msg);

		selectItem = new SelectItem("Server");

		// selectItem = new SelectItem();
		// selectItem.setTitle("Select");
		// selectItem.setHint("<nobr>A SelectItem with icons</nobr>");
		final DynamicForm form = new DynamicForm();
		form.setWidth(250);

		final IButton connectButton = new IButton("Connect");
		connectButton.setWidth(100);
		connectButton.setShowRollOver(true);
		connectButton.setShowDisabled(true);
		connectButton.setShowDown(true);
		connectButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				serverService.findById(Integer.parseInt((String) selectItem.getValue()), new AsyncCallback<ServerDto>() {

					public void onSuccess(ServerDto serv) {

						server = serv;
						// System.out.println("--" + serv.getNameServer());

						credentialsService.findCredentialByServer(serv, new AsyncCallback<AwsCredentialsDto>() {

							public void onSuccess(AwsCredentialsDto cred) {
								// System.out.println(cred.getAwsAccessKeyId());
								credentials = cred;
								hide();
								new MainPage(server.getTypeServer(), server, credentials, u);
							}

							public void onFailure(Throwable arg0) {
								// TODO Auto-generated method stub
							}
						});
					}

					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub

					}
				});
			}
		});

		final IButton deleteButton = new IButton("Delete");
		deleteButton.setWidth(100);
		deleteButton.setShowDisabled(true);
		deleteButton.setShowDown(true);
		deleteButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {

				serverService.findById(Integer.parseInt((String) selectItem.getValue()), new AsyncCallback<ServerDto>() {

					public void onSuccess(ServerDto serv) {

						System.out.println(serv.getIdServer());
						serverService.remove(serv, new AsyncCallback<Void>() {

							public void onSuccess(Void arg0) {

								SC.say("Server Deleted");
								ds.destroy();
								getData(u);
								selectItem.redraw();

							}

							public void onFailure(Throwable arg0) {
								// TODO Auto-generated method stub

							}
						});
					}

					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub

					}
				});

			}
		});

		form.setFields(selectItem);

//		serverService.findServerByUser(u, new AsyncCallback<List<ServerDto>>() {
//
//			public void onSuccess(List<ServerDto> list) {
//				for (ServerDto serverDto : list) {
//					System.out.println(serverDto.getNameServer());
//					// selectItem.setValues(serverDto.getNameServer());
//				}
////				ds = new DataSource();
//				if (ds.isCreated()) {
//					ds.destroy();
//				}
//				ds = ServerLocalDS.getInstance(list);
//
//				ListGridField nameServerField = new ListGridField("nameServer", "nameServer");
//				ListGridField ipServerField = new ListGridField("ipServer", "ipServer");
//
//				selectItem.setOptionDataSource(ds);
//				selectItem.setDefaultToFirstOption(true);
//				selectItem.setWidth(240);
//				selectItem.setTitle("Server");
//				selectItem.setValueField("idServer");
//				selectItem.setDisplayField("nameServer");
//				selectItem.setPickListWidth(450);
//				selectItem.setPickListFields(nameServerField, ipServerField);
//
//			}
//
//			public void onFailure(Throwable arg0) {
//			}
//		});

		serverlistLayout.addMember(form);
		serverlistLayout.addMember(connectButton);
//		serverlistLayout.addMember(deleteButton);

		/****************************************************************************************/
		HLayout layout = new HLayout();
		layout.setTop(30);
		layout.addMember(windowSectionStack);

		// layout.draw();
		// addMember(windowSectionStack);
		addMember(layout);
		selectItem.redraw();
	}

	private void getData(UserDto u) {
		
		serverService.findServerByUser(u, new AsyncCallback<List<ServerDto>>() {

			public void onSuccess(List<ServerDto> list) {
				for (ServerDto serverDto : list) {
					System.out.println(serverDto.getNameServer());

					// selectItem.setValues(serverDto.getNameServer());

				}
				ds = ServerLocalDS.getInstance(list);

				ListGridField nameServerField = new ListGridField("nameServer", "nameServer");
				ListGridField ipServerField = new ListGridField("ipServer", "ipServer");

				selectItem.setOptionDataSource(ds);
				selectItem.setDefaultToFirstOption(true);
				selectItem.setWidth(240);
				selectItem.setTitle("Server");
				selectItem.setValueField("idServer");
				selectItem.setDisplayField("nameServer");
				selectItem.setPickListWidth(450);
				selectItem.setPickListFields(nameServerField, ipServerField);

			}

			public void onFailure(Throwable arg0) {
			}
		});
	}
}
